//
//  AppUtility.m
//  iLiteNote
//
//  Created by xiangbc on 16/4/7.
//  Copyright © 2016年 LiteNice. All rights reserved.
//

#import "AppUtility.h"

@implementation AppUtility

+ (BOOL) isCryptFileWithData:(NSData *)data {
    if (data.length < 10) {
        return NO;
    }
    const char *tag = "VimCrypt~0";
    NSData *tag2 = [NSData dataWithBytes:tag length:10];
    
    if ([[data subdataWithRange:NSMakeRange(0, 10)] isEqualToData:tag2]) {
        return YES;
    }
    return NO;
}

+ (BOOL) isCryptFileWithFileName:(NSString *)filename {
    NSData *textData = [NSData dataWithContentsOfFile:filename];
    return [AppUtility isCryptFileWithData:textData];
}

+ (BOOL) isIPad {
    if (ScreenWidth >= 768) {
        return YES;
    }
    return NO;
}

+ (long long) fileSizeAtPath:(NSString*) filepath {
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filepath]) {
        return [[manager attributesOfItemAtPath:filepath error:nil] fileSize];
    }
    return 0;
}

+ (float) folderSizeAtPath:(NSString*) folderPath {
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) 
        return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil) {
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

+ (BOOL)moveFolder:(NSString*)dstPath src:(NSString*)srcPath {
    //NSFileManager *manager = [NSFileManager defaultManager];
    //manager move
    return YES;
}

+ (BOOL)deleteFolder:(NSString*)path {
    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *err = nil;
    BOOL r = [manager removeItemAtPath:path error:&err];
    if (r) {
        return YES;
    } else {
        NSLog(@"removeResult:%@", err);
        return NO;
    }
}
@end
